//
//  GYMainTabBarController.m
//  GoodLuck
//
//  Created by apple on 2018/1/26.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainTabBarController.h"
#import "GYMainBottomToolView.h"
#import "GYKeyViewController.h"
#import "GYLockViewController.h"
#import "GYTaskViewController.h"
#import "GYJournalViewController.h"
#import "GYPersonalViewController.h"
#import "GYBluetoothViewController.h"



@interface GYMainTabBarController ()

/** 正在显示的控制器 */
@property (nonatomic, strong) UIViewController *showingController;
@end

@implementation GYMainTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    //初始化
    [self setupBottomToolView];
    
    //添加子控制器
    [self addChildViewControllers];
}

- (void)setupBottomToolView {
    //1.添加底部工具栏
    GYMainBottomToolView *bottomToolView = [[GYMainBottomToolView alloc] initWithFrame:CGRectMake(0, self.view.height-120-(iPhoneX ? 44 : 0), ScreenWidth, 120)];
    __weak typeof(self) weakSelf = self;
    bottomToolView.bottomToolViewClickCallBack = ^(NSInteger currentIndex) {
        if (currentIndex < 5) {
            //1.更换控制器(首先移除原先的控制器)
            [weakSelf.showingController.view removeFromSuperview];
            weakSelf.showingController = weakSelf.childViewControllers[currentIndex-1];
            [weakSelf.view addSubview:weakSelf.showingController.view];
            weakSelf.showingController.view.frame = CGRectMake(currentIndex == 1 ? 10 : 0, 50, ScreenWidth-(currentIndex == 1 ? 20 : 0), ScreenHeight-50-130-(iPhoneX ? 44 : 0));
            if (currentIndex == 1) {
                weakSelf.showingController.view.layer.cornerRadius = 4;
                weakSelf.showingController.view.layer.masksToBounds = YES;
            }
        }else {
            //代表蓝牙和用户中心界面
            if (currentIndex == 101) {
                //蓝牙界面
                GYBluetoothViewController *bluetoothController = [[GYBluetoothViewController alloc] initWithNibName:@"GYBluetoothViewController" bundle:nil];
                [self presentViewController:bluetoothController animated:YES completion:nil];
            }else {
                //个人中心
                GYPersonalViewController *personalController = [[GYPersonalViewController alloc] initWithNibName:@"GYPersonalViewController" bundle:nil];
                [self presentViewController:personalController animated:YES completion:nil];
            }
        }
    };
    [self.view addSubview:bottomToolView];
}

/**
 添加子控件
 */
- (void)addChildViewControllers {
    //钥匙控制器
    GYKeyViewController *keyController = [[GYKeyViewController alloc] init];
    [self addChildViewController:keyController];
    //第一次复制
    self.showingController = keyController;
    
    //锁具控制器
    GYLockViewController *lockController = [[GYLockViewController alloc] init];
    [self addChildViewController:lockController];
    
    //任务控制器
    GYTaskViewController *taskController = [[GYTaskViewController alloc] init];
    [self addChildViewController:taskController];
    
    //日志控制器
    GYJournalViewController *journalController = [[GYJournalViewController alloc] init];
    [self addChildViewController:journalController];
    
    //添加正在显示的view
    [self.view addSubview:self.showingController.view];
    self.showingController.view.frame = CGRectMake(10, 50, ScreenWidth-20, ScreenHeight-50-130-(iPhoneX ? 44 : 0));
    self.showingController.view.layer.cornerRadius = 4;
    self.showingController.view.layer.masksToBounds = YES;
}

@end
